<template>
  <div>
    <el-row :gutter="10" class="mb8" style="border-bottom:1px solid #02A7F0;padding-bottom:5px;">
      <el-col>
          <span style="color:#02A7F0;font-size:18px;display:inline-block;padding-top:2px;"><b>成本信息</b></span>
          <div style="float:right;">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >新增</el-button>
              <el-button
                  type="warning"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  :loading="exportLoading"
                  @click="handleExport"
                  >导出</el-button>
          </div>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="costList">
      <el-table-column label="成本类型" align="center" prop="costType" :formatter="costTypeFormat"/>
      <el-table-column label="成本金额" align="center" prop="costSum" :formatter="numberFormat"/>
      <el-table-column label="录入人" align="center" prop="createBy" />
      <el-table-column label="录入时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改合同成本对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="成本类型" prop="costType">
          <el-select v-model="form.costType" placeholder="请选择成本类型" style="width:100%">
            <el-option v-for="dict in costTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
          </el-select>
        </el-form-item>
        <el-form-item label="成本金额" prop="costSum">
          <el-input-number v-model="form.costSum" placeholder="请输入成本金额" :max="999999999.99" style="width:100%"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCost, getCost, delCost, addCost, updateCost, exportCost } from "@/api/business/cost";
import { getContract} from "@/api/business/contract"; 

export default {
  name: "CostManage",
  props: {
    editConfig: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 合同成本表格数据
      costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 999,
        contractId: null,
        costType: null,
        costSum: null,
      },
      contractId: '',
      costTypeOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        costType: [
          { required: true, message: "成本类型不能为空", trigger: "change" }
        ],
        costSum: [
          { required: true, message: "成本金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("bus_cost_type").then(response => {
      this.costTypeOptions = response.data;
    });
  },
  mounted(){
      this.queryParams.contractId = this.editConfig.id
      this.contractId = this.editConfig.id
      this.getList()
  },
  methods: {
    numberFormat(row, column, cellValue, index){
        return cellValue.toLocaleString();
    },
    // 菜单状态字典翻译
    costTypeFormat(row, column) {
      return this.selectDictLabel(this.costTypeOptions, row.costType);
    },
    /** 查询合同成本列表 */
    getList() {
      this.loading = true;
      listCost(this.queryParams).then(response => {
        this.costList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        costId: null,
        contractId: null,
        costType: null,
        costSum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    // /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getList();
    // },
    // /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.costId)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const costId = row.costId || this.ids
      getCost(costId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.costId != null) {
            updateCost(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.contractId = this.contractId
            addCost(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const costIds = row.costId || this.ids;
      this.$confirm('是否确认删除成本数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCost(costIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同成本数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCost(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
